package auto;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class LogIn extends JDialog {

	/**
		 * 
		 */
	private static final long serialVersionUID = 1L;

	GridBagLayout grBagLayout = new GridBagLayout();
	GridBagConstraints grBagConstraints = new GridBagConstraints();

	private JPanel glavniPanel = null;
	private JPanel gornjiPanel = null;
	private JPanel donjiPanel = null;
	char[] charArray = null;
	private JLabel usernameLabel = null;
	private JTextField usernameTextField = null;

	private JLabel passLabel = null;
	private JPasswordField passTextField = null;

	private JButton okButton = null;
	private JButton cancelButton = null;

	private int fieldWidth = 150;

	Toolkit kit = Toolkit.getDefaultToolkit();
	Dimension dim = kit.getScreenSize();

	Smena1Panel s1pnl;
	Smena2Panel s2pnl;

	public LogIn(Smena1Panel s1, Smena2Panel s2) {
		this();
		s1pnl = s1;
		s2pnl = s2;
	}

	public LogIn() {
		pokreni();
		pack();
		setLocation((dim.width - getWidth()) / 2,
				(dim.height - getHeight()) / 2);
	}

	public void pokreni() {

		setTitle("Projekat");
		setContentPane(getGlavniPanel());

	}

	private void initGBConst(int x, int y) {

		grBagConstraints.gridx = x;
		grBagConstraints.gridy = y;
		grBagConstraints.insets = new Insets(5, 5, 5, 5);
	}

	public JPanel getGornjiPanel() {
		if (gornjiPanel == null) {
			gornjiPanel = new JPanel(grBagLayout);
			gornjiPanel.add(getUsernameLabel());
			gornjiPanel.add(getUsernameTextField());
			gornjiPanel.add(getPassLabel());
			gornjiPanel.add(getPassTextField());
		}
		return gornjiPanel;
	}

	public JPanel getDonjiPanel() {

		if (donjiPanel == null) {
			donjiPanel = new JPanel();
			donjiPanel.add(getOkButton());
			donjiPanel.add(getCancelButton());
		}
		return donjiPanel;
	}

	LogIn li = this;
	Boolean valid = false;

	public JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton("OK");
			okButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					Metode mtd = new Metode();
					mtd.validacija(UzmiUsername(), UzmiPassword(), li);
					if (valid == true) {
						dispose();
					}

				}
			});

		}
		return okButton;
	}

	public JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton("Cancel");
			cancelButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					dispose();

				}
			});
		}
		return cancelButton;
	}

	private JPanel getGlavniPanel() {
		if (glavniPanel == null) {
			glavniPanel = new JPanel();
			BorderLayout br = new BorderLayout();
			glavniPanel.setLayout(br);
			glavniPanel.add(getGornjiPanel(), BorderLayout.NORTH);
			glavniPanel.add(getDonjiPanel(), BorderLayout.SOUTH);

		}
		return glavniPanel;
	}

	public JLabel getUsernameLabel() {
		if (usernameLabel == null) {
			usernameLabel = new JLabel("Username:");
			initGBConst(0, 0);
			grBagLayout.setConstraints(usernameLabel, grBagConstraints);
		}
		return usernameLabel;
	}

	public JTextField getUsernameTextField() {
		if (usernameTextField == null) {
			usernameTextField = new JTextField();
			usernameTextField.setPreferredSize(new Dimension(fieldWidth, 20));
			initGBConst(1, 0);
			grBagLayout.setConstraints(usernameTextField, grBagConstraints);

		}
		return usernameTextField;
	}

	public String UzmiUsername() {
		String user = getUsernameTextField().getText();
		return user;

	}

	public JLabel getPassLabel() {
		if (passLabel == null) {
			passLabel = new JLabel("Password:");
			initGBConst(0, 1);
			grBagLayout.setConstraints(passLabel, grBagConstraints);
		}

		return passLabel;
	}

	public String UzmiPassword() {
		charArray = getPassTextField().getPassword();
		String pass = new String(charArray);
		return pass;

	}

	public JPasswordField getPassTextField() {

		if (passTextField == null) {
			passTextField = new JPasswordField();
			passTextField.setPreferredSize(new Dimension(fieldWidth, 20));
			passTextField.setEchoChar('*');
			initGBConst(1, 1);
			grBagLayout.setConstraints(passTextField, grBagConstraints);
		}

		return passTextField;
	}

	public void podaci(boolean ispravnost, int korisnikID, String prezime,
			String ime, int ulogaID) {
		valid = ispravnost;
		if (ispravnost) {
			if (Var.smenalog == 1) {
				Var.ime1 = ime;
				Var.ispravnost1 = ispravnost;
				Var.korisnikID1 = korisnikID;
				Var.prezime1 = prezime;
				Var.ulogaID1 = ulogaID;
				Var.smena1 = 1;
				s1pnl.setLabelIme(ime + " " + prezime);
			} else {
				Var.ime2 = ime;
				Var.ispravnost2 = ispravnost;
				Var.korisnikID2 = korisnikID;
				Var.prezime2 = prezime;
				Var.ulogaID2 = ulogaID;
				Var.smena2 = 2;
				s2pnl.setLabelIme(ime + " " + prezime);
			}
		} else {
			JOptionPane.showMessageDialog(null, "Pogresni podaci!");
		}
	}

}
